package com.phantasy.caches
{
	import com.phantasy.globals.GameParameters;
	import com.phantasy.ui.UISystem;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	public class CacheURLLoader extends EventDispatcher
	{
		public var url:String;
		public var datas:ByteArray;
		private var urlLoader:URLLoader;
		private var tryCount:int = 3;
		private var hastryTime:int = 0;
		private var urlKey:String;
		public function CacheURLLoader()
		{
			this.urlLoader = new URLLoader();
			this.urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			this.addListener();
		}
		
		private function addListener():void
		{
			this.urlLoader.addEventListener(Event.COMPLETE, this.completeHandler);
			this.urlLoader.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
			this.urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.securityErrorHandler);
			this.urlLoader.addEventListener(ProgressEvent.PROGRESS, this.progressHandler);
		}
		
		private function removeListener():void
		{
			this.urlLoader.removeEventListener(Event.COMPLETE, this.completeHandler);
			this.urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
			this.urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.securityErrorHandler);
			this.urlLoader.removeEventListener(ProgressEvent.PROGRESS, this.progressHandler);
		}
		
		private function completeHandler(event:Event):void
		{
			var _local2:ByteArray;
			this.datas = (this.urlLoader.data as ByteArray);
			if (((CacheManager.getInstance().allowCache) && (!((this.urlKey == "")))))
			{
				_local2 = CacheManager.getInstance().isExistResource(this.urlKey);
				if (_local2 == null)
				{
					CacheManager.getInstance().writeFile(this.urlKey, this.datas);
				}
			}
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void
		{
			if (this.hastryTime >= this.tryCount)
			{
				dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
			}
			else
			{
				this.hastryTime++;
				this.urlLoader.load(new URLRequest(this.url));
			}
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void
		{
			if (this.hastryTime >= this.tryCount)
			{
				dispatchEvent(new SecurityErrorEvent(SecurityErrorEvent.SECURITY_ERROR));
			}
			else
			{
				this.hastryTime++;
				this.urlLoader.load(new URLRequest(this.url));
			}
		}
		
		protected function progressHandler(event:ProgressEvent):void
		{
			dispatchEvent(event.clone());
		}
		
		public function load(request:URLRequest):void
		{
			if (request.url.indexOf("?") == -1)
			{
				request.url = ((request.url + "?v=") + UISystem.v);
			}
			this.urlKey = request.url;
			request.url = ((GameParameters.getInstance().resourceHost + "/") + request.url);
			this.url = request.url;
			if (CacheManager.getInstance().allowCache)
			{
				CacheManager.getInstance().readFile(this.urlKey, this.readCompleteHandler);
			}
			else
			{
				this.urlLoader.load(request);
			}
		}
		
		public function set dataFormat(_arg1:String):void
		{
			this.urlLoader.dataFormat = _arg1;
		}
		
		public function get byteloaded():int
		{
			return this.urlLoader.bytesLoaded;
		}
		
		public function get data():*
		{
			if (this.urlLoader.data)
			{
				return this.urlLoader.data;
			}
			return this.datas;
		}
		
		public function close():void
		{
			this.urlLoader.close();
		}
		
		public function reset():void
		{
			this.hastryTime = 0;
		}
		
		public function dispose():void
		{
			this.removeListener();
		}
		
		private function readCompleteHandler(data:ByteArray):void
		{
			if (data == null)
			{
				this.urlLoader.load(new URLRequest(this.url));
			}
			else
			{
				this.datas = data;
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
	}
}